/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Dao;

import com.lcfgly.Database.UserInfo;
import com.lcfgly.DatabaseFactory.DataFactoryInterface;
import com.lcfgly.Interface.GetCacheMark;
import com.lcfgly.Interface.UpdateCacheMark;
import java.io.Serializable;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *  Users表操作
 * @author lcfgly
 */
@Repository
public class UserImpl implements Serializable{
    
    private final String table="user_info";
    private final String sqlheader="SELECT uid,username,password,solt,uname,start FROM user_info ";

    @Autowired
        @Qualifier("databaseshoprice")
    private DataFactoryInterface dataFactory;
    
    /**
     * 更新用户
     * @param arg0
     * @param userid 用户ID
     * @return
     * @throws SQLException 
     */
    @UpdateCacheMark(value = {"shopuserbyusername_${0.username}","userbyid_${1}","userinfobyid_${1}","shopuserinfoshopbyid_${1}","shopuserinfobyusername_${0.username}"})
    public int update(UserInfo arg0,int userid) throws SQLException {
        return dataFactory.update(table,arg0, "uid=?", userid);
    }
    
    
    /**
     * 检测用户名是否被用
     * @param username
     * @return 
     * @throws java.sql.SQLException 
     */
    public UserInfo usernameCheck(String username)throws SQLException
    {
        String sql=sqlheader+" WHERE username=? ";
        return dataFactory.one(UserInfo.class,sql,username);
    }
    
    /**
     * 按用户名查找数据
     * @param username
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopuserinfobyusername_${0}")
    public UserInfo findShopbyusername(String username)throws SQLException
    {
        String sql=sqlheader+" WHERE username = ? AND role = 0 ";
        return dataFactory.one(UserInfo.class,sql,username);
    }
    
    /**
     * 按照ID获取用户信息
     * @param id
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopuserinfoshopbyid_${0}")
    public UserInfo findShopById(int id) throws SQLException{
        String sql=sqlheader+" WHERE uid=? AND role = 0 AND start = 1 ";
        return dataFactory.one(UserInfo.class,sql, id);
    }
    
    /**
     * 按照ID获取用户信息
     * @param id
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("userinfobyid_${0}")
    public UserInfo findById(int id) throws SQLException{
        String sql=sqlheader+" WHERE uid = ? AND start = 1 ";
        return dataFactory.one(UserInfo.class,sql, id);
    }
    
    /**
     * 插入数据
     * @param arg
     * @return
     * @throws SQLException 
     */
    public int insertBackID(UserInfo arg)throws SQLException{
        return dataFactory.insert(arg, table);
    }
    
}
